package com.restphp.java.utils.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.restphp.java.utils.string.XSSFilterUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * TimeUtils.
 *
 * @author zj.
 *         Created on 2018/8/15 0015.
 */
public class MapOperateUtils {
    /**
     * 获取Map String类型值.
     * @param source
     * @param key
     * @return
     */
    public static String getStr(Map source, String key) {
        return  String.valueOf(source.get(key));
    }
    /**
     * 获取Map Int类型值.
     * @param source
     * @param key
     * @return
     */
    public static Integer getInt(Map source, String key) {
        return Integer.valueOf(String.valueOf(source.get(key)));
    }

    /**
     * 获取Map Float类型值.
     * @param source
     * @param key
     * @return
     */
    public static Float getFloat(Map source, String key) {
        return Float.valueOf(String.valueOf(source.get(key)));
    }

    /**
     * 获取Map Long类型数据值.
     * @param source
     * @param key
     * @return
     */
    public static Long getLong(Map source, String key) {
        return Long.valueOf(String.valueOf(source.get(key)));
    }

    /**
     * XSS转义过滤.
     * @param map 由com.alibaba.fastjson转换出的Map.
     * @return
     */
    public static Map filterXSS(Map<String, Object> map) {
        Map<String, Object> newMap = new HashMap<>();
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            Object val = entry.getValue();
            if(val == null){
                continue;
            }
            String valClass = val.getClass().getName();
            if ("java.lang.String".equals(valClass)) {
                val = filterXSS(val.toString());
            } else if ("com.alibaba.fastjson.JSONObject".equals(valClass)) {
                val = filterXSS((JSONObject) val);
            } else if ("com.alibaba.fastjson.JSONArray".equals(valClass)) {
                JSONArray jsonArry = (JSONArray) val;
                val = filterXSS(jsonArry);
            }

            newMap.put(key, val);
        }
        return newMap;
    }

    /**
     * XSS转义过滤.
     * @param val
     * @return
     */
    public static String filterXSS(String val) {
        return XSSFilterUtils.filter(val);
    }

    /**
     * XSS转义过滤.
     * @param val
     * @return
     */
    public static JSONObject filterXSS(JSONObject val) {
        Map<String, Object> jsonNode = JSONObject.parseObject(JSONObject.toJSONString(val), Map.class);
        Map newJsonNode = filterXSS(jsonNode);
        return JSONObject.parseObject(JSONObject.toJSONString(newJsonNode));
    }

    /**
     * XSS转义过滤.
     * @param val
     * @return
     */
    public static JSONArray filterXSS(JSONArray val) {
        JSONArray newVal = new JSONArray();
        for (Object jsonObject : val) {
            String valClass = jsonObject.getClass().getName();
            if ("java.lang.String".equals(valClass)) {
                newVal.add(filterXSS(jsonObject.toString()));
            } else if ("com.alibaba.fastjson.JSONObject".equals(valClass)) {
                newVal.add(filterXSS((JSONObject) jsonObject));
            } else if ("com.alibaba.fastjson.JSONArray".equals(valClass)) {
                JSONArray jsonArry = (JSONArray) jsonObject;
                newVal.add(filterXSS(jsonArry));
            } else {
                newVal.add(jsonObject);
            }
        }
        return newVal;
    }

    /*public static void main(String[] args) {
        String p = "{\"asd\":\"我是测试<script>alert('did')</script>\"}";
        Map<String, Object> testMap = JSONObject.parseObject(p, Map.class);
        Map result = filterXSS(testMap);
        System.out.println(result);
    }*/
}
